<script setup lang="ts">
  import { FormInstance } from 'element-plus'
  import { useVisibleTypeMultipleUpdate } from '@/services/pages/good/useType'
  import { PropType, ref } from 'vue'
  import { HMessageBox } from '@/components/common/message'

  const { updateForm, updateLoading, update, resetForm } = useVisibleTypeMultipleUpdate()

  defineOptions({
    name: 'GoodTypeVisibleTypeMultipleUpdateModal',
  })

  const props = defineProps({
    idList: {
      type: Array as PropType<Array<string>>,
      required: true,
    },
  })

  const emits = defineEmits<{
    (e: 'update'): void
  }>()

  // 弹窗显示状态
  const visible = defineModel('visible', { type: Boolean, default: false })

  // 表单校验规则
  const rules = ref({
    visibilityType: [{ required: true, message: '请选择可见类型', trigger: ['change', 'blur'] }],
  })

  // 表单组件实例
  const formInstance = ref<FormInstance | null>(null)

  /**
   * 隐藏弹窗
   */
  const hideDialog = () => {
    visible.value = false
    resetForm()
  }

  /**
   * 确定事件
   */
  const onConfirm = async () => {
    if (formInstance.value) {
      try {
        await formInstance.value.validate()
        const res = await update(props.idList)
        if (res.code === 20000) {
          emits('update')
          hideDialog()
          return
        }
        HMessageBox.error(res.message)
      } catch (error) {
        // error
      }
    }
  }
</script>

<template>
  <h-dialog v-model="visible" title="修改可见类型" :loading="updateLoading">
    <el-form
      ref="formInstance"
      class="modal_form_content"
      label-position="top"
      :model="updateForm"
      :rules="rules"
    >
      <el-form-item label="可见类型" prop="visibilityType">
        <el-select v-model="updateForm.visibilityType" placeholder="请选择可见类型">
          <el-option :value="1" label="全部可见" />
          <el-option :value="2" label="网点可见" />
          <el-option :value="3" label="公司可见" />
          <el-option :value="4" label="小程序不可见" />
        </el-select>
      </el-form-item>
    </el-form>

    <template #footer>
      <h-button is-modal type="primary" @click="onConfirm">确定</h-button>
      <h-button is-modal @click="hideDialog">取消</h-button>
    </template>
  </h-dialog>
</template>
